package com.yw.android.letsgo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;

import com.yw.android.common.base.BaseFragment;
import com.yw.android.common.base.WebViewActivity;
import com.yw.android.common.utils.KVUtils;
import com.yw.android.common.utils.PreferenceUtil;
import com.yw.android.common.utils.TextUtils;
import com.yw.android.common.utils.ToastHelper;
import com.yw.android.letsgo.adapt.MainFragAdapter;
import com.yw.android.letsgo.fragment.breed.BreedFragment;
import com.yw.android.letsgo.fragment.home.HomeFragment;
import com.yw.android.letsgo.fragment.market.MarketFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements BaseFragment.OnFragmentInteractionListener {
    private List<Fragment> listFragment;//存储页面对象
    private ViewPager viewPager;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_market:
                    viewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_breed:
                    viewPager.setCurrentItem(1);
                    return true;
                case R.id.navigation_home:
                    return true;
            }
            return false;
        }
    };

    @MainThread
    @CallSuper
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    public void initView() {
        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        viewPager = findViewById(R.id.fragment_container);
        //向ViewPager添加各页面
        listFragment = new ArrayList<>();
        listFragment.add(MarketFragment.newInstance(null));
        listFragment.add(BreedFragment.newInstance(null));
        listFragment.add(MarketFragment.newInstance(null));
        MainFragAdapter myAdapter = new MainFragAdapter(getSupportFragmentManager(), this, listFragment);
        viewPager.setAdapter(myAdapter);

        String cookie = PreferenceUtil.getInstance().getString("cookie", "");
        if (TextUtils.empty(cookie)) {
            Intent i = WebViewActivity.getIntent(this, "百度登陆", HomeFragment.LOGIN_URL);
            startActivityForResult(i, 100);
        }
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100) {
            if (resultCode == Activity.RESULT_OK) {
                String cookie = KVUtils.get(data, "cookie", "");
                if (!TextUtils.empty(cookie)) {
                    ToastHelper.getInstance().shortToast(MainActivity.this, R.string.login_succeed);
                    PreferenceUtil.getInstance().putString("cookie", cookie);
                } else {
                    ToastHelper.getInstance().shortToast(MainActivity.this, R.string.login_failed);
                }
            }
        }
    }
}
